package com.zxl.grocery.modular.crawler.processor;

import com.zxl.grocery.modular.crawler.base.AbstractPageProcessor;
import com.zxl.grocery.modular.crawler.base.Constant;
import com.zxl.grocery.modular.crawler.entity.CrawlerBeautiful;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;

/**
 * info:wall美图爬虫
 * Created by Lzx on 2017/7/12.
 */
@Component
public class WallProcessor extends AbstractPageProcessor {

    public static final String list = "http://www.3gbizhi.com/lists-%E5%85%A8%E9%83%A8/\\w+.html";

    public static final String item = "http://www.3gbizhi.com/\\w+/\\w+.html";

    @Override
    public String[] initTargetUrl() {
        return new String[]{"http://www.3gbizhi.com/lists-%E5%85%A8%E9%83%A8/1.html"};
    }

    @Override
    public void process(Page page) {
        if (page.getUrl().regex(list).match()) {
            page.addTargetRequests(page.getHtml().xpath("//div[@class='leftc']/ul/li").links().regex(item).all());
        } else {
            CrawlerBeautiful beautiful = new CrawlerBeautiful();
            beautiful.setLink(page.getHtml().xpath("//div[@class='imgtitlec']").links().toString());
            beautiful.setSourcesId(Constant.Sources_3GBiZhi);
            if (StringUtils.isNotEmpty(beautiful.getLink())) {
                page.putField("beautiful", beautiful);
            }
        }

    }

}
